define(['jquery','cookie','user','util','footer'],function($,cookie,user,util,footer){
	var baseUrl='/index.php/',
	    wxKey = 'SOWXOPENID',
	    timeout=10000,
	    order,
	    qrcode,
	    wxid=$.cookie(wxKey);
	$.ajaxSetup({
	   dataType: 'json',
	   timeout:timeout,
	   complete:function(res,status){
	   	  var data;
	      if(status=='success'&&res.readyState==4&&res.status==200&&res.responseJSON){
	          data=res.responseJSON;
	          if(data.errorcode===-3004||data.retcode==='2'){
	             user.loginOut();
	          }else if(data.retcode&&data.retcode!='0'){
	             console.log(util.getError(data.retcode));
	          }
	      }
	   }
	})
	
    return {
    	setOrder:function(o){
    	   order=o;
    	},
    	getOrder:function(o){
    	   return order;
    	},
    	userInfo:function(cb){
    		var url=util.url('user');
    	    $.ajax({
    		    url:url,
    		    timeout:120000,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='用户信息加载失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	coupons:function(cb){
    		var url=util.url('coupons');
    	    $.getJSON(url,function(rs){
    	        cb(rs);
    	    })
    	},
    	notice:function(cb){
    		var url=util.url('notice');
    	    $.getJSON(url,{count:1},function(rs){
    	        cb(rs);
    	    })
    	},
    	noticeRead:function(id){
    		$.getJSON(util.url('noticeback'),{noticeid:id},function(rs){
    	    })
    	},
    	init:function(boot){
    		footer.init();
    		if(location.hash.split('/')[1]=='agent'){
    			$.cookie('agentid',location.hash.split('/')[2]);
    		}
    		util.isWxBrowser()?this.initWx(boot):boot(); 
    	},
    	initFooter:footer.setFocus,
    	footerShow:footer.setShow,
    	initWx:function(boot){
		    var txUrl=function(appid){
		    		var target=window.wxProxy?(window.wxProxy+'?backUrl='+location.href):location.href;
			    	return 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+encodeURIComponent(target)+'&response_type=code&scope=snsapi_base#wechat_redirect'
			    },
			    result,
			    that=this,
			    appid=$.cookie('appid'),
		        location=window.location,
		        host = window.wxdomain?window.wxdomain:location.hostname,
	            queryObj=util.queryToObj(location.search.substring(1));
		   	if(wxid){
		   		boot();
		   	}else if(queryObj.code){
		   		 util.loadMsg('微信openid获取中……');
		   		 $.ajax({
				    url:util.url('wxid'),
				    dataType:'json',
				    data:{wxcode:queryObj.code,wxdomain:host},
				    success:function(rs){
				    	util.loadMsg('');
				    	if(rs.retcode==='0'){
			    	    	user.setIsRegister(rs.exist);
			    	    	wxid=rs.wxid;
			    	    	if(rs.exist==='1'){
			    	    	   user.data.mobile=rs.data.mobile;
			    	    	   $.cookie('mobile',user.data.mobile);
			    	    	   user.data.uid=rs.data.uid;
			    	    	}
							$.cookie(wxKey,wxid,{expires:7});
			    	    	boot();
				    	}else{
				    	    util.msg('获取微信id失败！');
				    	}
				    },
				    error:function(){
				        util.loadMsg('');
				    }
				});
		   	}else {
		   		if(appid){
		   			window.location.href=txUrl(appid);
		   		}else{
		            that.appid(function(rs){
				    	if(rs.retcode=='0'){
				    	    if(rs.appid){
	                            appid=rs.appid;
	                            $.cookie('appid',appid,{expires:7});
	                            $.cookie('parentid',rs.parentid,{expires:7});
	                            window.location.href=txUrl(appid);
				    	    }
				    	}else if(rs.retcode=='19'){
				    	    util.msg('系统appid不存在，请联系管理员！');
				    	}
					});	
		   		}
			}
		},
		isReg:function(o){
			 $.ajax({
	   			url:util.url('isReg'),
	   			data:{wxid:$.trim(wxid)},
	   			method:'post',
	   			dataType:'json',
	   			success:o.success,
	   			error:o.error
	   		});
		},
		getWxid:function(){
		   return wxid;
		},
    	login:function(data,cb){
    	    $.ajax({
    	      url:util.url('login'),
    	      data:data,
    	      method:'get',
    	      dataType:'json',
    	      timeout:timeout,
    	      beforeSend: function () {
		          $("#loginBtn").attr("disabled", true).val('登录中……');
		          
		      },
		      error: function () {
	              util.msg("网络异常请再试试");
	              $("#loginBtn").removeAttr("disabled").val('登录');
	          },
    	      success:cb
    	    });
    	},
    	appid:function(cb){
    		var host = window.wxdomain?window.wxdomain:location.hostname;
    		util.loadMsg('系统信息获取中6');
    	    $.ajax({
			    url:util.url('appid'),
			    dataType:'json',
			    data:{
			       wxdomain:host
			    },
			    complete:function(){
			       util.loadMsg('');
			    },
			    success:cb
			});	
    	},
    	orders:function(data,cb){
    	   $.ajax({
    	      url:util.url('orders'),
    	      data:data,
    	      method:'get',
    	      dataType:'json',
    	      success:cb
    	    });
    	},
    	ordersn:function(data,cb,error){
    	   $.ajax({
    	      url:util.url('ordersn'),
    	      data:data,
    	      method:'get',
    	      dataType:'json',
    	      success:cb,
    	      error:error
    	    });
    	},
    	order:function(opensn,cb){
    	   $.ajax({
    	      url:util.url('orders'),
    	      data:{o:opensn},
    	      method:'get',
    	      dataType:'json',
    	      success:cb
    	    });
    	},
    	setTitle:util.setTitle,
    	getWxid:function(){
    		return $.cookie(wxKey);
    	},
    	getGoods:function(){
    		var data;
    		$.ajax({
    			url:util.url('goods'),
    		    async:false,
    		    dataType:'json',
    		    success:function(rs){
    		    	data=rs;
    		    }
    		})
    		return data;
    	},
    	goodsInfo:function(cb){
    		var data;
    		$.ajax({
    			url:util.url('goods'),
    			timeout:timeout,
    		    dataType:'json',
    		    success:function(rs){
    		    	cb(rs)
    		    },
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载商品数据失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	showQrcode:function(){
    	   var host = window.wxdomain?window.wxdomain:location.host,
    	       isSubscribe=false,
    	       showCode=false;
    	   if(!util.isWxBrowser())return false;
    	   if(!user.isRegister()||$.cookie('subscribe'))return false;//先注册后关注
    	   $.ajax({
    			url:util.url('subscribe'),
    		    async:false,
    		    dataType:'json',
    		    data:{wxid:$.cookie('SOWXOPENID'),wxdomain:host},
    		    success:function(rs){
    		    	if(rs.retcode==='0'){
    		    	    if(rs.subscribe=='1'){
    		    	     	//处理用户头像
    		    	    	$.cookie('headimgurl',rs.headimgurl);
    		    	    	user.data.headimgurl=rs.headimgurl;
    		    	    	$.cookie('nickname',rs.nickname);
    		    	    	user.data.name=rs.nickname;
    		    	    	$.cookie('subscribe',1);
    		    	        isSubscribe=true;   		    	     
    		    	    }
    		    	}else{
    		    	   util.msg('获取用户头像信息失败'+rs.retcode);
    		    	   isSubscribe=true;
    		    	}
    		    }
    	   });
    	   if(isSubscribe)return false;
    	   $.ajax({
    			url:util.url('qrcode'),
    		    data:{wxdomain:host},
    		    async:false,
    		    dataType:'json',
    		    success:function(rs){
    		    	if(rs.retcode==='0'){
    		    	    if(rs.qrcode&&rs.qrcode.qrcode!=''){
    		    	    	qrcode=rs.qrcode.qrcode;
    		    	    	location.hash='#/index/subscribe';
    		    	    }else if(rs.url&&rs.url!=''){
    		    	        location.href=rs.url;
    		    	    }
    		    	    showCode = true;
    		    	}
    		    }
    	   })
    	   return showCode;
    	},
    	subscribe:function(cb){
    	   var host = window.wxdomain?window.wxdomain:location.host;
    	   $.ajax({
    			url:util.url('subscribe'),
    		    async:false,
    		    dataType:'json',
    		    data:{wxid:$.cookie('SOWXOPENID'),wxdomain:host},
    		    success:cb
    	   });
    	},
    	getQrcode:function(){
    	   if(qrcode)return qrcode;
    	},
    	noticeList:function(data,cb){
    	    $.getJSON(util.url('noticeList'),data,cb)
    	},
    	customer:function(data,cb){
    	    $.getJSON(util.url('customer'),data,cb)
    	},
    	articleList:function(data,cb){
    	    $.getJSON(util.url('articleList'),data,cb)
    	},
    	noticeDetail:function(data,cb){
    	    $.getJSON(util.url('noticeDetail'),data,cb)
    	},
    	articleDetail:function(data,cb){
    	    $.getJSON(util.url('articleDetail'),data,cb)
    	},
    	createOrder:function(o){
    		var params={
	    		   tradetype:49,
	    		   validtype:51,
	    		   ordermode:49,
	    		   operatorid:2,
	    		   differpt:-1,
	    		   operatortype:50,
	    		   tpslpricetype:49
	    		},
	    		data=util.queryToObj(o.data);
	        data.direct=data.direct.charCodeAt(0);
    		$.extend(params,data);
    	    $.ajax({
    	       url:util.url('createOrder'),
    	       data:params,
    	       dataType: 'json',
	           beforeSend:o.beforeSend,
    	       success:function(rs){
    	       	   o.cb(rs);
    	       }
    	    })
    	},
    	closeOrder:function(data,cb){
    		var params={
	    		};
	    		
    		$.extend(params,data);
    	    $.getJSON(util.url('closeOrder'),params,cb)
    	},
    	closeOrderAll:function(cb){
    		var params={
    		   ordersn:'-1',
    		   orderamount:1
    		}
    	    $.getJSON(util.url('closeOrderAll'),params,cb)
    	},
    	couponsAll:function(cb){
    		$.ajax({
    		    url:util.url('couponsAll'),
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载赢家券数据失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	allowFund:function(cb){
    		$.ajax({
    		    url:util.url('allowFund'),
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载可用金额失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	channels:function(cb){
    		$.ajax({
    		    url:util.url('channels'),
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载通道失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	userchannels:function(cb){
    		$.ajax({
    		    url:util.url('userchannels'),
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载用户通道失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	historyOrders:function(data,cb){
    		$.ajax({
    		    url:util.url('historyOrder'),
    		    data:data,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载历史交易失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	historyOrder:function(opensn,cb){
    		$.ajax({
    		    url:util.url('historyOrder'),
    		    data:{o:opensn},
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载历史交易失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	closeOrders:function(data,cb){
    		$.ajax({
    		    url:util.url('closeOrders'),
    		    data:data,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载当前平仓交易失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	modifyOrder:function(data,cb){
    		$.ajax({
    		    url:util.url('modifyOrder'),
    		    data:data,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='修改止盈止损失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	fundFlow:function(data,cb){
    		$.ajax({
    		    url:util.url('fundFlow'),
    		    data:data,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载收支名细失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	fundflowHistory:function(data,cb){
    		$.ajax({
    		    url:util.url('fundflowHistory'),
    		    data:data,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载收支名细失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	privinces:function(cb){
    		$.ajax({
    		    url:util.url('privinces'),
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载省会数据失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	banks:function(cb){
    		$.ajax({
    		    url:util.url('banks'),
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载开户行数据失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	captcha:function(cb){
    		$.ajax({
    		    url:util.url('captcha'),
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载验证码失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	citys:function(id,cb){
    		$.ajax({
    		    url:util.url('citys'),
    		    data:{provincecode:id},
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载开户城市数据失败！';
    		    	util.msg(error);
    		    }
    		})
    	},
    	charge:function(data,cb){
    		$.ajax({
    		    url:util.url('charge'),
    		    data:data,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='';
    		    	util.msg(error);
    		    }
    		})
    	},
    	cash:function(data,cb){
    		$.ajax({
    		    url:util.url('cash'),
    		    data:data,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='';
    		    	util.msg(error);
    		    }
    		})
    	},
    	sendSMS:function(o){
    	    $.ajax({
    		    url:util.url('sms'),
    		    data:{
    		       mobile:o.mobile,
    		       imgcode:o.code
    		    },
    		    timeout:120000,
    		    success:function(rs){
    		       if(rs.retcode!='0'){
				       util.dealErrorCode(rs.retcode);
				   } 
    		       o.callback(rs);  
    		    },
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='发送短信失败！';
    		    	util.msg(error);
    		    	o.error();
    		    }
    		});
    	},
    	bind:function(o,cb){
    	    $.ajax({
    		    url:util.url('bind'),
    		    data:o,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='绑定失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	unBind:function(o,cb){
    	    $.ajax({
    		    url:util.url('unbind'),
    		    data:o,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='解绑失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	register:function(o,cb){
    	    $.ajax({
    		    url:util.url('register'),
    		    data:o,
    		    method:'post',
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='注册失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	commission:function(o,cb){
    	    $.ajax({
    		    url:util.url('commission'),
    		    data:o,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='查询失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	pwdCheck:function(o,cb){
    	    $.ajax({
    		    url:util.url('pwdCheck'),
    		    data:o,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='验证失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	changePwd:function(o,cb){
    	    $.ajax({
    		    url:util.url('changePwd'),
    		    data:o,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='修改失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	resetPwd:function(o){
    	    $.ajax({
    		    url:util.url('resetPwd'),
    		    data:o.data,
    		    success:o.success,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='重置失败！';
    		    	util.msg(error);
    		    	o.error();
    		    }
    		});
    	},
    	wxinfo:function(code,cb){
    		var host = this.getHost();
    	    $.ajax({
    		    url:util.url('wxinfo'),
    		    data:{code:code,wxdomain:host},
    		    timeout:120000,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='用户信息加载失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	getHost:function(){
    		return window.wxdomain?window.wxdomain:location.host;
    	},
    	charts:function(o){
    	    $.ajax({
    		    url:util.url('charts'),
    		    data:o.data,
    		    success:o.callback,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载图表失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	points:function(o,cb){
    		$.ajax({
    		    url:util.url('points'),
    		    data:o,
    		    success:cb,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='积分信息加载失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	userPoints:function(cb){
    		$.ajax({
    		    url:util.url('userPoints'),
    		    success:cb,  
    		    timeout:300000,
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='用户积分信息加载失败！';
    		    	util.msg(error);
    		    }
    		});
    	},
    	mobileToWxid:function(o){
    	    $.ajax({
    		    url:util.url('mobileToWxid'),
    		    data:o.data,
    		    success:o.success,    
    		    error:function(xhr,status){
    		    	var error=status=='timeout'?'网络超时，':'';
    		    	error+='加载用户信息失败！';
    		    	util.msg(error);
    		    }
    		});
    	}
    }
})